/**
 * @description 路由拦截状态管理，目前两种模式：all模式与intelligence模式，其中partialRoutes是菜单暂未使用
 */
import { asyncRoutes, constantRoutes } from "@/router";
import { getRouterList, getResourceTypeList } from "@/api/router";
import { convertRouter, filterAsyncRoutes } from "@/utils/handleRoutes";

const state = { routes: [], partialRoutes: [], resourceTypeList: [] };
const getters = {
  routes: (state) => state.routes,
  partialRoutes: (state) => state.partialRoutes,
};
const mutations = {
  setRoutes(state, routes) {
    state.routes = constantRoutes.concat(routes);
  },
  setAllRoutes(state, routes) {
    state.routes = constantRoutes.concat(routes);
  },
  setPartialRoutes(state, routes) {
    state.partialRoutes = constantRoutes.concat(routes);
  },
  // setResourceTypeList(state, resourceTypeList) {
  //   state.resourceTypeList = resourceTypeList;
  //   console.log(state.resourceTypeList);
  //   let childrenRouter = [];
  //   state.resourceTypeList.forEach((element) => {
  //     const resourceTypeName = element.resourceTypeName;
  //     const name =
  //       resourceTypeName.slice(0, 1).toUpperCase + resourceTypeName.slice(1);
  //     this.$router.options.rputes[1].children.push({
  //       name: resourceTypeName,
  //       path: resourceTypeName,
  //       component: () => import("@/views/test/helloworld"),
  //     });
  //     let children = {
  //       path: resourceTypeName,
  //       name: resourceTypeName,
  //       component: () => import("@/views/test/helloworld"),
  //       meta: {
  //         title: element.resourceTypeTitle,
  //       },
  //     };
  //     childrenRouter.push(children);
  //   });
  //   asyncRoutes.addRoutes({
  //     path: "/resourceManage",
  //     component: () => import("@/layouts"),
  //     redirect: "noRedirect",
  //     name: "ResourceManage",
  //     alwaysShow: true,
  //     meta: { title: "资源管理", icon: "user" },
  //     children: childrenRouter,
  //   });
  //   let resourceListRouter = {
  //     path: "/resourceManage",
  //     component: () => import("@/layouts"),
  //     redirect: "noRedirect",
  //     name: "ResourceManage",
  //     alwaysShow: true,
  //     meta: { title: "资源管理", icon: "user" },
  //     children: childrenRouter,
  //   };
  //   asyncRoutes.push(resourceListRouter);
  //   console.log("路由：" + JSON.stringify(resourceListRouter));
  //   mutations.setRoutes(state, asyncRoutes);
  // },
};
const actions = {
  async setRoutes({ commit }, permissions) {
    //开源版只过滤动态路由permissions，admin不再默认拥有全部权限
    const finallyAsyncRoutes = await filterAsyncRoutes(
      [...asyncRoutes],
      permissions
    );
    commit("setRoutes", finallyAsyncRoutes);
    return finallyAsyncRoutes;
  },
  async setAllRoutes({ commit }) {
    let { data } = await getRouterList();
    data.push({ path: "*", redirect: "/404", hidden: true });
    let accessRoutes = convertRouter(data);
    commit("setAllRoutes", accessRoutes);
    return accessRoutes;
  },
  setPartialRoutes({ commit }, accessRoutes) {
    commit("setPartialRoutes", accessRoutes);
    return accessRoutes;
  },
  // async setResourceTypeList({ commit }) {
  //   let { data } = await getResourceTypeList();
  //   commit("setResourceTypeList", data);
  // },
};
export default { state, getters, mutations, actions };
